package com.stock.service.Impl;



import com.stock.mapper.StockMapper;
import com.stock.mapper.StockWarningConfigMapper;
import com.stock.pojo.Stock;
import com.stock.pojo.StockWarningConfig;
import com.stock.service.StockService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class StockServiceImpl implements StockService {

    private final StockMapper stockMapper;
    private final StockWarningConfigMapper stockWarningConfigMapper;

    public StockServiceImpl(StockMapper stockMapper, StockWarningConfigMapper stockWarningConfigMapper) {
        this.stockMapper = stockMapper;
        this.stockWarningConfigMapper = stockWarningConfigMapper;
    }

    @Override
    public void decreaseStock(String productId, int quantity) throws IllegalArgumentException {
        Stock stock = stockMapper.getStockByProductId(productId);
        if (stock == null) {
            throw new IllegalArgumentException("商品不存在库存记录");
        }
        if (stock.getStockQuantity() < quantity) {
            throw new IllegalArgumentException("库存不足，无法扣减");
        }
        stockMapper.decreaseStock(productId, quantity);
    }

    @Override
    public void increaseStock(String productId, int quantity) {
        stockMapper.increaseStock(productId, quantity);
    }

    @Override
    public int getStockQuantity(String productId) {
        Stock stock = stockMapper.getStockByProductId(productId);
        return stock!= null? stock.getStockQuantity() : 0;
    }

    @Override
    public void setStockWarningThreshold(String productId, int threshold) {
        StockWarningConfig config = stockWarningConfigMapper.getConfigByProductId(productId);
        if (config == null) {
            config = new StockWarningConfig();
            config.setProductId(productId);
            config.setWarningThreshold(threshold);
            config.setIsEnabled(true);
            stockWarningConfigMapper.insert(config);
        } else {
            config.setWarningThreshold(threshold);
            stockWarningConfigMapper.updateConfigById(config);
        }
    }

    @Override
    public boolean isBelowStockWarning(String productId) {
        StockWarningConfig config = stockWarningConfigMapper.getConfigByProductId(productId);
        if (config!= null && config.getIsEnabled()) {
            int currentStock = getStockQuantity(productId);
            return currentStock < config.getWarningThreshold();
        }
        return false;
    }

    @Override
    public void batchDecreaseStock(List<String> productIds, List<Integer> quantities) {
        if (productIds.size()!= quantities.size()) {
            throw new IllegalArgumentException("商品 ID 列表和数量列表长度不一致");
        }
        for (int i = 0; i < productIds.size(); i++) {
            try {
                decreaseStock(productIds.get(i), quantities.get(i));
            } catch (IllegalArgumentException e) {
                // 可以记录日志或者进行更友好的业务提示，此处简单打印异常信息示例
                System.err.println("批量扣减库存时出现异常: " + e.getMessage());
            }
        }
    }

    @Override
    public List<StockWarningConfig> getStockWarningConfigs(List<String> productIds) {
        return stockWarningConfigMapper.getConfigsByProductIds(productIds);
    }
}